package cn.com.controller;

import cn.com.dao.UserDao;
import cn.com.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * <p>Project:Academic - LoginController
 *
 * @author 黄俊玮 On Administrator
 * @version 1.0
 * @DataTime 2024/6/14 08:55:08
 * @since 1.8
 */
public class LoginController {
    private static UserDao userDao = new UserDao();

    public void goLogin (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
        System.out.println("进入了goLogin");
        int phone = Integer.parseInt(request.getParameter("phone"));
        String pwd = request.getParameter("pwd");
        User u = userDao.find(phone,pwd);
        if (u == null){
            request.setAttribute("err","<span style=\"color:red\">账户密码错误</span>");
            request.getRequestDispatcher("/index.do").forward(request,response);
            System.out.println("密码错误");
            return;
        }
        System.out.println("true");
        request.getSession().setAttribute("loginState",u);
        request.getRequestDispatcher("/index.do").forward(request,response);

    }
    public void goLogout (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
        request.getSession().invalidate();
        request.getRequestDispatcher("/index.do").forward(request, response);
    }
}
